import axios from 'axios';
import { Message } from '@arco-design/web-vue';

const pro = (import.meta.env as any).MODE === 'production';

const baseUrl = pro ? window.globalConfig.BASE_URL : '';

const service = axios.create({
  baseURL: baseUrl, // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 60000, // request timeout
  // headers: { "Content-Type": "application/json" },
});

service.interceptors.request.use(
  async (config) => {
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  },
);
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  (response) => {
    const res = response.data;
    const statusCode = response.status;

    if (statusCode !== 200) {
      Message.error(res.message);
      return Promise.reject(new Error(res.message || 'Error'));
    }

    return res;
  },
  (error) => {
    Message.error(error.message);
    return Promise.reject(error);
  },
);

export default service;
